﻿using System.Configuration;
using System.Diagnostics;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using Wise.TimeWarrior.DTO;
using Wise.TimeWarrior.Helpers;
using Wise.TimeWarrior.Interfaces;
using Wise.TimeWarrior.OSFunctionalitiesProxy;

namespace Wise.TimeWarrior.Impl
{
    /// <summary>
    /// <see cref="ISystemUsage"/>
    /// </summary>
    public class SystemUsageService : ISystemUsage
    {
        private List<UserEventDto> events = new List<UserEventDto>();
        private IUserSessionSystemEvents sessionSystemEvents;


        /// <summary>
        /// .ctor
        /// </summary>
        public SystemUsageService(IUserSessionSystemEvents sessionSystemEvents)
        {

            this.sessionSystemEvents = sessionSystemEvents;
        }
        /// <summary>
        /// <see cref="ISystemUsage.StartWatchForUserStatus"/>
        /// </summary>
        public void StartWatchForUserStatus()
        {
            this.sessionSystemEvents.SessionSwitched += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            SystemEvents_SessionSwitch(null, SessionSwitchReasonExtended.ProgramStarted);
        }

        /// <summary>
        /// <see cref="ISystemUsage.StopWatchForUserStatus"/>
        /// </summary>
        public void StopWatchForUserStatus()
        {
            SystemEvents_SessionSwitch(null, SessionSwitchReasonExtended.ProgramStopped);
            this.sessionSystemEvents.SessionSwitched -= new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
        }



        /// <summary>
        /// <see cref="ISystemUsage.Events"/>
        /// </summary>
        List<UserEventDto> ISystemUsage.Events
        {
            get
            {
                lock (this)
                {
                    return events;
                }
            }
        }


        /// <summary>
        /// <see cref="ISystemUsage.ClearEventData"/>
        /// </summary>
        public void ClearEventData()
        {

            lock (this)
            {
                events.Clear();
            }
        }

        public void DoSnapshoot()
        {
            lock (this)
            {
                DateTime eventTime = DateTime.Now;

                Process[] proc = Process.GetProcesses();

                List<ProcessDto> processDtos = proc.Select(process => ProcessDto.Create(
                    GetStartDateForProcessIfAccessGranted(process),
                    process.ProcessName,
                    process.MainWindowTitle
                    )).ToList();
                //because default type for argument is SessionSwitchReason its needed to allow passing object
               
                    events.Add(UserEventDto.Create(eventTime, SessionSwitchReasonExtended.Manual, processDtos));
               
                
            }
        }

        private void SystemEvents_SessionSwitch(object sender, object e)
        {
            lock (this)
            {
                DateTime eventTime = DateTime.Now;

                Process[] proc = Process.GetProcesses();

                List<ProcessDto> processDtos = proc.Select(process => ProcessDto.Create(
                    GetStartDateForProcessIfAccessGranted(process),
                    process.ProcessName,
                    process.MainWindowTitle
                    )).ToList();
                //because default type for argument is SessionSwitchReason its needed to allow passing object
                if (e is SessionSwitchReasonExtended)
                {
                    events.Add(UserEventDto.Create(eventTime, (SessionSwitchReasonExtended)e, processDtos));
                }
                else if (e is SessionSwitchEventArgs)
                {
                    SessionSwitchReasonExtended sessionSwitchReasonExtended = SessionSwitchEnumHelper.ConverToSessionSwitchReasonExtended(((SessionSwitchEventArgs)e).Reason);
                    events.Add(UserEventDto.Create(eventTime, sessionSwitchReasonExtended, processDtos));
                }
                else
                {
                    throw new SettingsPropertyWrongTypeException("e :" + e.GetType());
                }
            }
        }

        private static DateTime? GetStartDateForProcessIfAccessGranted(Process process)
        {
            try
            {
                return process.StartTime;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
